package org.jeecgframework.poi.util;

import java.io.InputStream;
import java.net.URL;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiExcelUtil {

	
	public static Workbook toWorkbook(String path) throws Exception {
		URL url = new URL(path);
		InputStream file = url.openStream();
		Workbook wb = null;
		try {
			// 2003
			wb = new HSSFWorkbook(file);
		} catch (Exception e) {
			file.close();
			// 2007
			try {
				file = url.openStream();
				wb = new XSSFWorkbook(file);
			} catch (Exception e1) {
		/*		file.close();
				// 重新测试一次 ，更改后缀
				// 计划重新上传，并改后缀
				String key = OSSFileUtils.getKey(path, null);
				String[] split = StringUtils.split(key, ".");
				StringBuffer sb = new StringBuffer();
				sb.append(split[0]);
				sb.append("_c");
				if (split[1].equals("xls")) {
					sb.append(".xlsx");
				} else {
					sb.append(".xls");
				}

				String copy = OSSFileUtils.copy(key, sb.toString());
				url = new URL(copy);
				file = url.openStream();
				try {
					wb = new HSSFWorkbook(file);
				} catch (Exception ex) {
					wb = new XSSFWorkbook(file);
				}*/
			}
		}

		return wb;
	}
	
}
